// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/webui/projector_app/projector_app_client.h"

#include "base/check_op.h"
#include "base/values.h"

namespace ash {

namespace {

constexpr char kPendingScreencastName[] = "name";
constexpr char kPendingScreencastUploadProgress[] = "uploadProgress";
constexpr int64_t kPendingScreencastDiffThresholdInBytes = 600 * 1024;

ProjectorAppClient* g_instance = nullptr;
}  // namespace

base::Value PendingScreencast::ToValue() const {
  base::Value val(base::Value::Type::DICTIONARY);
  val.SetKey(kPendingScreencastName, base::Value(name));
  DCHECK_GT(total_size_in_bytes, 0);
  const double upload_progress = static_cast<double>(bytes_transferred) /
                                 static_cast<double>(total_size_in_bytes);
  val.SetKey(kPendingScreencastUploadProgress,
             base::Value(upload_progress * 100));
  return val;
}

bool PendingScreencast::operator==(const PendingScreencast& rhs) const {
  // When the bytes of pending screencast didn't change a lot (less than
  // kPendingScreencastDiffThresholdInBytes), we consider this pending
  // screencast doesn't change. It helps to reduce the frequency of updating the
  // pending screencast list.
  return container_dir == rhs.container_dir &&
         std::abs(bytes_transferred - rhs.bytes_transferred) <
             kPendingScreencastDiffThresholdInBytes;
}

bool PendingScreencastSetComparator::operator()(
    const PendingScreencast& a,
    const PendingScreencast& b) const {
  return a.container_dir < b.container_dir ||
         a.bytes_transferred < b.bytes_transferred;
}

// static
ProjectorAppClient* ProjectorAppClient::Get() {
  DCHECK(g_instance);
  return g_instance;
}

ProjectorAppClient::ProjectorAppClient() {
  DCHECK_EQ(g_instance, nullptr);
  g_instance = this;
}

ProjectorAppClient::~ProjectorAppClient() {
  DCHECK_EQ(g_instance, this);
  g_instance = nullptr;
}

}  // namespace ash
